import requests
import re
from lxml import etree
import random
import parsel

# 发起请求
def post_request(url=None):
    # 如果啥也不传就是评论url
    if url == None:
        # 网址
        url = 'http://www.dianping.com/shop/H9FNPpCqj1Tu98oD/review_all'
        # 请求头
        headers = {
            'Connection': 'keep-alive',
            'Host': 'www.dianping.com',
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36',
            'Cookie': 'fspop=test; cy=1305; cye=huaiyang; _lxsdk=181aa7ad838c8-03fafa0ef11406-26021a51-154ac4-181aa7ad838c8; _lxsdk_cuid=181aa7ad838c8-03fafa0ef11406-26021a51-154ac4-181aa7ad838c8; _hc.v=492fd566-e5d1-725b-d6a8-df2ece63c8df.1656422587; s_ViewType=10; dper=35e3a1c591e245be209e4ebd5830e3c4171c5892b304e9c3b7213e1902b777f0716c0a139036324bf35dae94b160677df284f026b15ff9fcb176396112fd73b1; ll=7fd06e815b796be3df069dec7836c3df; Hm_lvt_602b80cf8079ae6591966cc70a3940e7=1656422587,1656467024,1656542870; _lx_utm=utm_source=Baidu&utm_medium=organic; WEBDFPID=8454y3x6vx225zy811w7uwy7v9208x76818ywuuv73097958119z9029-1656836472401-; Hm_lpvt_602b80cf8079ae6591966cc70a3940e7=1656913554; _lxsdk_s=181c7aad8b1-48e-530-49e||203'
        }
    else:
        # 访问css与svg专用请求头
        headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36',
        }

    # 获取响应
    r = requests.get(url=url, headers=headers)
    # r = requests.get(url=url, headers=headers, proxies=ip)
    # print(f'能用的ip地址：{ip}')
    print("访问成功")
    # 获取文本
    text = r.text.encode("gbk", "ignore").decode("gbk", "ignore")  # 解决报错双重严格限制



    return text


print(post_request())